
package com.ukao.steward.pesenter.valetRunners;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ukao.steward.base.BasePresenter;
import com.ukao.steward.bean.SearchUserBean;
import com.ukao.steward.bean.StringBean;
import com.ukao.steward.bean.VipCardListBean;
import com.ukao.steward.bean.addressBean;
import com.ukao.steward.consts.Constant;
import com.ukao.steward.consts.SaveParams;
import com.ukao.steward.retrofit.ApiCallback;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.util.GetJsonDataUtil;
import com.ukao.steward.util.T;
import com.ukao.steward.view.valetRunners.TransactVipView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class TransactVipPesenter extends BasePresenter<TransactVipView> {


    public TransactVipPesenter(TransactVipView view) {
        attachView(view);
    }


    private ArrayList<addressBean> options1Items = new ArrayList<addressBean>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();




    public  void  getAddressProvinceData(Context _mActivity ) {



        String JsonData = new GetJsonDataUtil().getJson(_mActivity, "province.json");
        Gson mGson = new Gson();
        ArrayList<addressBean> provinceCityLists = mGson.fromJson(JsonData,
                new TypeToken<List<addressBean>>() {
                }.getType());

        options1Items = (ArrayList<addressBean>) provinceCityLists;

        for (int i = 0; i < provinceCityLists.size(); i++) {    //遍历省份
            ArrayList<String> CityList = new ArrayList<>();//该省的城市列表（第二级）
            ArrayList<ArrayList<String>> Province_AreaList = new ArrayList<>();//该省的所有地区列表（第三极）

            for (int c = 0; c < provinceCityLists.get(i).getChildren().size(); c++) {  //遍历该省份的所有城市
                String CityName = provinceCityLists.get(i).getChildren().get(c).getLabel();
                CityList.add(CityName);//添加城市
                ArrayList<String> City_AreaList = new ArrayList<>();//该城市的所有地区列表

                //如果无地区数据，建议添加空字符串，防止数据为null 导致三个选项长度不匹配造成崩溃
                if (provinceCityLists.get(i).getChildren().get(c).getChildren() == null
                        || provinceCityLists.get(i).getChildren().get(c).getChildren().size() == 0) {
                    City_AreaList.add("");
                } else {

                    for (int d = 0; d < provinceCityLists.get(i).getChildren().get(c).getChildren().size(); d++) {//该城市对应地区所有数据
                        String AreaName = provinceCityLists.get(i).getChildren().get(c).getChildren().get(d).getLabel();

                        City_AreaList.add(AreaName);//添加该城市所有地区数据
                    }
                }
                Province_AreaList.add(City_AreaList);//添加该省所有地区数据
            }

            /**
             * 添加城市数据
             */
            options2Items.add(CityList);

            /**
             * 添加地区数据
             */
            options3Items.add(Province_AreaList);


        }
        if (CheckUtils.isNull(mvpView))return;
        mvpView.cityDataSucceed(provinceCityLists,options1Items,options2Items,options3Items);
    }



    /**
     查询会员卡
     */
    public void searchVipCard(final boolean  ishowDialog ) {
        if (ishowDialog){
            mvpView.showLoading();
        }

        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.userCard(Constant.createParameter(mHashMap)),
                new ApiCallback<VipCardListBean>() {
                    @Override
                    public void onSuccess(VipCardListBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.vipCardSucceed(model.getData(),ishowDialog);
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }

    /**
     * 新增用户
     * @param newparams
     */
    public void addUserInfo( HashMap<String,String> newparams   ) {

        mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.addUserInfo(Constant.createParameter(newparams)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {

                        if (model.getHttpCode() == 200) {
                            mvpView.addUserInfoSucceed(model.getMsg());
                        }else{

                            mvpView.showError(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });

    }
    /**
     * 手机号搜索
     *
     * @param keyword
     */
    public void queryKeyword(final  String keyword) {
            mvpView.showLoading();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("keyword",keyword);
        mHashMap.put("accessToken", SaveParams.getToken());
        addSubscription(apiStores.queryKeywordUse(Constant.createParameter(mHashMap)),
                new ApiCallback<SearchUserBean>() {
                    @Override
                    public void onSuccess(SearchUserBean model) {

                        if (model.getHttpCode() == 200) {

                            SearchUserBean mSearchUserBean=model.getData();
                            if (mSearchUserBean.getIsRegister()!=0){/*注册过得用户*/
                                mvpView.queryKeyword(mSearchUserBean);
                            }else{/*未注册*/

                            }


                        } else {

                            T.show(model.getMsg());

                        }

                    }


                    @Override
                    public void onFinish() {

                        mvpView.hideLoading();

                    }
                });


    }





}
